using System;
using System.Collections.Generic;
using OracleInternal.Common;
using OracleInternal.ServiceObjects;

namespace OracleInternal.TTC.Accessors
{
	internal class TTCRawAccessor : Accessor
	{
		internal TTCRawAccessor(ColumnDescribeInfo colMetaData, MarshallingEngine marshallingEngine, bool bForBind)
			: base(colMetaData, marshallingEngine, bForBind)
		{
			if (!bForBind)
			{
				InitForDataAccess(m_colMetaData.m_maxLength);
			}
		}

		internal override byte[] GetByteRepresentation(int currentRow)
		{
			byte[] array = null;
			int num = m_totalLengthOfData[currentRow];
			if (num > 0)
			{
				List<ArraySegment<byte>> list = m_RowDataSegments[currentRow];
				if (list != null)
				{
					array = new byte[num];
					Accessor.CopyDataToUserBuffer(list, 0, array, 0, num);
				}
			}
			return array;
		}

		internal override byte[] GetByteRepresentation(DataUnmarshaller dataUnmarshaller, int currentRow, int columnIndex)
		{
			byte[] result = null;
			int length = 0;
			try
			{
				dataUnmarshaller.StartAccumulatingColumnData(currentRow, columnIndex, m_colDataSegments);
				dataUnmarshaller.UnmarshalCLR_ScanOnly(m_colMetaData.m_maxLength, ref length);
				if (length > 0)
				{
					result = new byte[length];
					Accessor.CopyDataToUserBuffer(m_colDataSegments, 0, result, 0, length);
					return result;
				}
				return result;
			}
			finally
			{
				m_colDataSegments.Clear();
				dataUnmarshaller.m_bAccumulateByteSegments = false;
				dataUnmarshaller.m_dataSegments = null;
			}
		}

		internal long GetDataInBuffer(DataUnmarshaller dataUnmarshaller, int currentRow, int columnIndex, long fieldOffset, byte[] buffer, int bufferOffset, int length)
		{
			long result = 0L;
			int length2 = 0;
			try
			{
				dataUnmarshaller.StartAccumulatingColumnData(currentRow, columnIndex, m_colDataSegments);
				dataUnmarshaller.UnmarshalCLR_ScanOnly(m_colMetaData.m_maxLength, ref length2);
				if (length2 > 0)
				{
					return Accessor.CopyDataToUserBuffer(m_colDataSegments, (int)fieldOffset, buffer, bufferOffset, length);
				}
				return result;
			}
			finally
			{
				m_colDataSegments.Clear();
				dataUnmarshaller.m_bAccumulateByteSegments = false;
				dataUnmarshaller.m_dataSegments = null;
			}
		}

		internal long GetDataLen(DataUnmarshaller dataUnmarshaller, int currentRow, int columnIndex)
		{
			int length = 0;
			try
			{
				dataUnmarshaller.StartAccumulatingColumnData(currentRow, columnIndex, m_colDataSegments);
				dataUnmarshaller.UnmarshalCLR_ScanOnly(m_colMetaData.m_maxLength, ref length);
			}
			finally
			{
				m_colDataSegments.Clear();
				dataUnmarshaller.m_bAccumulateByteSegments = false;
				dataUnmarshaller.m_dataSegments = null;
			}
			return length;
		}
	}
}
